<template>
	<!-- 
	swiper中的transfrom会使fixed失效,此时用height固定高度; 
	swiper中无法触发mescroll-mixins.js的onPageScroll和onReachBottom方法,只能用mescroll-uni,不能用mescroll-body
	-->
	<!-- ref动态生成: 字节跳动小程序编辑器不支持一个页面存在相同的ref (如不考虑字节跳动小程序可固定值为 ref="mescrollRef") -->
	<!-- top的高度等于悬浮菜单tabs的高度 -->
	<mescroll-uni :ref="'mescrollRef' + i" @init="mescrollInit" :height="height" :down="downOption" @down="downCallback" :up="upOption" @up="upCallback" @emptyclick="emptyClick">
		<!-- 数据列表 -->
		<u-gap height="20" bg-color="#F5F5F5"></u-gap>
		<view class="list uni-flex-column">
			<view class="item" v-for="(item, i) in list" :key="i">
				<view v-if="item.type==1" class="uni-flex-row" v-for="(data, index) in item.commodity" :key="index">
					<vimage class="img_box" :src="data.master" mode=""></vimage>
					<view class="uni-flex-column info_text">
						<text class="u-line-2">{{ data.commodity }}</text>
						<text class="sm-font">{{ data.pvs_value }}</text>
						<text class="pirce">{{ data.sell_price }}</text>
						<text>x{{ data.count }}</text>
					</view>
				</view>
				
				<view class="uni-flex-row" v-if="item.type==2&&item.commodity">
					<vimage class="img_box" :src="item.commodity.master" mode=""></vimage>
					<view class="uni-flex-column info_text">
						<text class="u-line-2">{{ item.commodity.name }}</text>
					<!-- 	<text class="sm-font">{{ item.commodity.pvs_value }}</text>
						<text class="pirce">{{ item.commodity.sell_price }}</text>
						<text>x{{ item.commodity.count }}</text> -->
					</view>
				</view>
				<view class="uni-flex-row uni-flex-title b_line sm-font">
					<text>订单号：</text>
					<text class="">{{ item.order_no }}</text>
				</view>
				<view class="uni-flex-row uni-flex-title b_line sm-font">
					<text>核销员编号：</text>
					<text>{{ item.assistant }}</text>
				</view>
				<view class="uni-flex-row uni-flex-title b_line sm-font">
					<text>核销时间：</text>
					<text>{{ item.accept_time }}</text>
				</view>
			</view>
		</view>
		<uni-login ref="Login" @callback="logincallback"></uni-login>
		<common v-if="showCommon"></common>
	</mescroll-uni>
</template>

<script>
import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js';
import MescrollMoreItemMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mixins/mescroll-more-item.js';
// import {apiGoods} from "@/api/mock.js"
import util from '@/common/util.js';
import http from '@/common/http.js';
export default {
	mixins: [MescrollMixin, MescrollMoreItemMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
	data() {
		return {
			downOption: {
				auto: false // 不自动加载 (mixin已处理第一个tab触发downCallback)
			},
			upOption: {
				auto: false, // 不自动加载
				page: {
					num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
					size: 10 // 每页数据的数量
				},
				noMoreSize: 10, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
				empty: {
					tip: '暂无数据', // 提示
					btnText: '',
					icon: util.getimgurl('static/images/mescroll-empty.png', '1')
				}
			},
			list: [], //列表数据
			hasShow: 'none',
			page: 1,
			size: 10,
			showCommon: false
		};
	},
	props: {
		i: Number, // 每个tab页的专属下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
		index: {
			// 当前tab的下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
			type: Number,
			default() {
				return 0;
			}
		},
		tabs: {
			// 为了请求数据,演示用,可根据自己的项目判断是否要传
			type: Array,
			default() {
				return [];
			}
		},
		height: [Number, String] // mescroll的高度
	},
	methods: {
		/*下拉刷新的回调 */
		downCallback() {
			// 这里加载你想下拉刷新的数据, 比如刷新轮播数据
			// loadSwiper();
			// 下拉刷新的回调,默认重置上拉加载列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
			this.mescroll.resetUpScroll();
		},
		/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
		upCallback(page) {
			//联网加载数据
			let keyword = this.tabs[this.i].name;
			this.getOrderList(page);

			// apiGoods(page.num, page.size, keyword).then(res=>{
			// 	//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
			// 	this.mescroll.endSuccess(res.list.length);
			// 	//设置列表数据
			// 	if(page.num == 1) this.goods = []; //如果是第一页需手动制空列表
			// 	this.goods=this.goods.concat(res.list); //追加新数据
			// }).catch(()=>{
			// 	//联网失败, 结束加载
			// 	this.mescroll.endErr();
			// })
		},
		//点击空布局按钮的回调
		emptyClick() {
			uni.showToast({
				title: '点击了按钮,具体逻辑自行实现'
			});
		},
		getOrderList(page) {
			let data = this.tabs[this.i];
			data.status = 'loading';
			const _this = this;

			const param = {
				page: page.num,
				size: page.size,
				type: data.type
			};
			// if (data.state) {
			// 	if (data.state == 12) {
			// 		param.state = 2;
			// 		param.order_type = 2;
			// 	} else {
			// 		param.state = data.state;
			// 	}
			// }
			this.$http('/accept/record/list?page=' + param.page + '&size=' + 10, param, 'POST').then(
				res => {
					const json = res.data.msg;
					this.mescroll.endSuccess(json.data.length);
					let list = json.data;
					list.forEach(a => {
						a.type=1;
						if(!Array.isArray(a.commodity)){
							a.type=2;
						}
						
					});					
		
					//设置列表数据
					if (page.num == 1) this.list = []; //如果是第一页需手动制空列表
					this.list = this.list.concat(list); //追加新数据
					//
					if (list.length < page.size) {
						this.showCommon = true;
					}
				},
				e => {
					uni.showModal({
						title: '提示',
						content: e,
						showCancel: false
					});
					this.mescroll.endErr();
				}
			);
		},
		logincallback() {
			if (e.islogin) {
				this.mescroll.resetUpScroll();
			}
		},
		topay(item) {
			let list = [];
			list.push(item.order_no);
			list = list.join(',');
			let money = item.allprice;

			uni.navigateTo({
				url: 'pay?total=' + money + '&list=' + list
			});
		},
		goDetail(obj) {
			if (obj.money_type == 1) {
				this.Jumpurl({
					link: '../order/orderdetail?type=4&id=' + obj.id,
					type: 1,
					login_type: 2
				});
			} else {
				this.Jumpurl({
					link: '/pointsmallpage/order/order?id=' + obj.id,
					type: 1,
					login_type: 2
				});
			}
		},
		togoods(id, type) {
			if (type == 1) {
				this.Jumpurl({
					type: 2,
					id: id
				});
			} else {
				this.Jumpurl({
					type: 1,
					link: '../goods/detail?id=' + id
				});
			}
		},
		cancelOrder(id) {
			const _this = this;
			uni.showModal({
				title: '是否确认取消？',
				success(res) {
					if (res.confirm) {
						const param = {
							id: id
						};
						http('/order/orderDel/' + id, {}, 'DELETE').then(
							res => {
								if (res.statusCode == 201) {
									_this.mescroll.resetUpScroll();
								} else {
									uni.showToast({
										title: res.data.msg,
										icon: 'none'
									});
								}
							},
							e => {
								uni.showModal({
									title: '提示',
									content: e,
									showCancel: false
								});
							}
						);
					}
				}
			});
		},
		delOrder(id) {
			const _this = this;
			const param = {
				id: id
			};
			http('/order/delete', param, 'POST').then(
				res => {
					const json = res.data;
					if (json.code == 0) {
						_this.mescroll.resetUpScroll();
					} else {
						uni.showModal({
							title: '提示',
							content: json.msg,
							showCancel: false
						});
					}
				},
				e => {
					uni.showModal({
						title: '提示',
						content: e,
						showCancel: false
					});
				}
			);
		},
		refundOrder(obj) {
			uni.navigateTo({
				url: '/pages/mine/order/detail?id=' + obj.id + '&refund=' + obj.refundState
			});
		},
		confirmOrder(obj) {
			const _this = this;
			uni.showModal({
				title: '是否确认收货？',
				success(res) {
					if (res.confirm) {
						let orderCommodity = obj.orderCommodity;
						let cids = [];
						orderCommodity.forEach(a => {
							if (a.status == 3) {
								cids.push(a.ocid);
							}
						});
						let param = {
							id: obj.id,
							cids: JSON.stringify(cids)
						};
						http('/order/confirm', param, 'PUT').then(res => {
							if (res.statusCode == 200) {
								_this.mescroll.resetUpScroll();
							} else {
							}
						});
					}
				}
			});
		},
		accMul(arg1, arg2) {
			var m = 0,
				s1 = arg1.toString(),
				s2 = arg2.toString();
			try {
				m += s1.split('.')[1].length;
			} catch (e) {}
			try {
				m += s2.split('.')[1].length;
			} catch (e) {}
			return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m);
		}
	}
};
</script>
<style lang="scss">
.uni-block-list {
	// padding-top: 100rpx !important;

	.order {
		padding-top: 240rpx;
		align-items: center;

		image {
			width: 50%;
			height: 300rpx;
			margin-bottom: 100rpx;
		}

		text {
			color: #a9a9a9;
			font-size: 26rpx;
		}
	}

	.list {
		.item {
			width: 710rpx;
			margin-left: 20rpx;
			padding: 20rpx 32rpx;
			border-radius: 12rpx;

			.head {
				padding: 0 0 20rpx 0;
			}

			.body {
				padding: 20rpx 0;

				.img_box {
					width: 140rpx;
					height: 140rpx;
					margin-right: 30rpx;
					border-radius: 8rpx;
				}

				.detail {
					flex: 1;
					justify-content: space-between;
					height: 130rpx;

					.title {
						justify-content: space-between;
						font-size: 36rpx;
						height: 130rpx;
						width: 380rpx;

						.skuvalue {
							color: #999;
							font-size: 26rpx;
						}
					}

					.price {
						align-self: flex-start;
						display: flex;
						flex-direction: column;
						line-height: 1;

						text:first-child {
							color: #000;
						}

						.num {
							margin-top: 20rpx;
							text-align: right;
							color: #999999;
							font-size: 24rpx;
						}
					}
				}
			}

			.bottom {
				padding: 10rpx 0;

				.left {
					width: 100%;
					justify-content: flex-end;

					.price {
						font-weight: bold;
						margin-right: 16rpx;
					}
				}

				.right {
					width: 100%;
					justify-content: flex-end;

					.number {
						font-weight: bold;
					}

					.price {
						font-weight: bold;
					}
				}
			}

			.button {
				justify-content: flex-end;
				margin-top: 18rpx;

				text {
					display: inline-block;
					text-align: center;
					font-size: 24rpx;
					line-height: 50rpx;
					// padding: 12rpx 0;

					width: 150rpx;
					border-radius: 30rpx;
					height: 50rpx;
				}

				/*cancel：取消*/
				.pay {
					color: #ff4f11;
					margin-left: 20rpx;
					border: 2rpx solid #ff4f11;
				}

				.finish {
					color: #ffffff;
					margin-left: 20rpx;
				}

				.success {
					color: #ffffff;
					margin-left: 20rpx;
				}

				.cancel {
					border: 1px solid #878787;
				}

				.refund {
					border: 1px solid #878787;
				}

				.del {
					border: 1px solid #878787;
				}

				.state {
					color: #b00606;
					width: 210rpx;
					text-align: right;
				}
			}
		}
	}
}
.list {
	.item {
		font-size: 30rpx;
		color: #333;
		// border-bottom: 1rpx solid #f5f5f5;
		width: 710rpx;
		margin: 20rpx;
		padding: 20rpx 32rpx;
		border-radius: 12rpx;

		background-color: #fff;
		.img_box {
			width: 180rpx;
			height: 180rpx;
			border-radius: 6rpx;
			margin-right: 26rpx;
		}
		.info_text {
			flex: 1;
			font-size: 28rpx;
			// justify-content: space-between;
			height: 180rpx;
			.u-line-2 {
				font-size: 26rpx;
				color: #333333;
				line-height: 46rpx;
				height: 92rpx;
			}
			// padding-left: 20rpx;
			.sm-font {
				font-size: 24rpx;
				color: #d1d1d1;
				margin: 10rpx 0;
			}
		}
		.iconfont {
			font-size: 40rpx;
		}
		.active {
			.iconfont {
				color: #ff3c29;
			}
		}
		.pirce {
			color: #ff3c29;
			font-size: 30rpx;
		}
		.b_line {
			padding: 20rpx 0;
			border-bottom: 1rpx solid #f4f4f4;
		}
	}
}
</style>
